﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.ObjectModel
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    /// <summary>
    /// The base class for all code units.
    /// </summary>
    public abstract class CodeUnitBase
    {
        private string mPath;
        private IEnumerable<MethodDefinitionNode> mMethods;
        private IEnumerable<ConstantStatement> mConstants;
        private IEnumerable<VariableDeclarationStatement> mVariables;

        /// <summary>
        /// Initializes a new instance of the <see cref="CodeUnitBase" /> class.
        /// </summary>
        /// <param name="path">The path to the source code file.</param>
        /// <param name="methods">The methods which belong to the code unit.</param>
        /// <param name="constants">The constants which belong to the code unit.</param>
        /// <param name="variables">The variables which belong to the code unit.</param>
        protected CodeUnitBase(string path, IEnumerable<MethodDefinitionNode> methods, IEnumerable<ConstantStatement> constants, IEnumerable<VariableDeclarationStatement> variables)
        {
            if (methods == null) throw new ArgumentNullException("methods");
            if (constants == null) throw new ArgumentNullException("constants");

            mPath = path;
            mMethods = new ReadOnlyCollection<MethodDefinitionNode>((methods as IList<MethodDefinitionNode>) ?? new List<MethodDefinitionNode>(methods));
            mConstants = new ReadOnlyCollection<ConstantStatement>((constants as IList<ConstantStatement>) ?? new List<ConstantStatement>(constants));
            mVariables = new ReadOnlyCollection<VariableDeclarationStatement>((variables as IList<VariableDeclarationStatement>) ?? new List<VariableDeclarationStatement>(variables));
        }

        /// <summary>
        /// Gets the path to the source code file.
        /// </summary>
        public string Path
        {
            get { return mPath; }
        }

        /// <summary>
        /// Gets the methods in the code unit.
        /// </summary>
        public IEnumerable<MethodDefinitionNode> Methods
        {
            get { return mMethods; }
        }

        /// <summary>
        /// Gets the constants in the code unit.
        /// </summary>
        public IEnumerable<ConstantStatement> Constants
        {
            get { return mConstants; }
        }

        /// <summary>
        /// Gets the variables in the code unit.
        /// </summary>
        public IEnumerable<VariableDeclarationStatement> Variables
        {
            get { return mVariables; }
        }
    }
}
